#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.10
#####

library(here)
library(data.table)
library(texreg)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}


# Create binary versions of heterogeneity vars ----------------------------

pooled[ , pid2_het := factor(pid2_het, levels = c("Independent", "Democrat", "Republican"))]
pooled[ , uni_degree := as.integer(edu2 %in% c("4 year degree", "Masters degree", "Professional Degree (JD, MD)", "Doctorate"))]
pooled[ , not_white := as.integer(race2 != "White/Caucasian")]
pooled[ , news_daily := as.integer(news2 == 7)]


# Fitting -----------------------------------------------------------------

int_pid <- lm(
  immPCA ~ CH*pid2_het + CS*pid2_het + NM*pid2_het + female + race2 + age + I(age^2) + edu2 + news2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )

int_age <- lm(
  immPCA ~ CH*I(age < 42) + CS*I(age < 42) + NM*I(age < 42) + female + race2 + edu2 + news2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )

int_gender <- lm(
  immPCA ~ CH*female + CS*female + NM*female+ female + race2 + age + I(age^2) + edu2 + news2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )

int_news <- lm(
  immPCA ~ CH*news_daily + CS*news_daily + NM*news_daily + female + race2 + age + I(age^2) + edu2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )

int_race <- lm(
  immPCA ~ CH*not_white + CS*not_white + NM*not_white + female + age + I(age^2) + edu2 + news2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )

int_edu <- lm(
  immPCA ~ CH*uni_degree + CS*uni_degree + NM*uni_degree + female + race2 + age + I(age^2) + news2 + region2 + factor(wave),
  data = pooled[replication == 0]
  )


# Reporting ---------------------------------------------------------------

texreg(
  list(int_gender,
       int_age,
       int_pid,
       int_edu,
       int_race,
       int_news),
  custom.coef.map = list(
    "NM" =  "Norms",
    "CS" = "Countering Stereotypes",
    "CH" = "Common Humanity",
    "female:NM" =  "Norms $\\times$ Female",
    "female:CS" = "Countering Stereotypes $\\times$ Female",
    "CH:female" = "Common Humanity $\\times$ Female",
    "I(age < 42)TRUE:NM" =  "Norms $\\times$ Age $<$ 42",
    "I(age < 42)TRUE:CS" = "Countering Stereotypes $\\times$ Age $<$ 42",
    "CH:I(age < 42)TRUE" = "Common Humanity $\\times$ Age $<$ 42",
    "pid2_hetDemocrat:NM" =  "Norms $\\times$ Democrat",
    "pid2_hetDemocrat:CS" = "Countering Stereotypes $\\times$ Democrat",
    "CH:pid2_hetDemocrat" = "Common Humanity $\\times$ Democrat",
    "pid2_hetRepublican:NM" =  "Norms $\\times$ Republican",
    "pid2_hetRepublican:CS" = "Countering Stereotypes $\\times$ Republican",
    "CH:pid2_hetRepublican" = "Common Humanity $\\times$ Republican",
    "uni_degree:NM" =  "Norms $\\times$ Uni. Degree",
    "uni_degree:CS" = "Countering Stereotypes $\\times$ Uni. Degree",
    "CH:uni_degree" = "Common Humanity $\\times$ Uni. Degree",
    "not_white:NM" =  "Norms $\\times$ Not White",
    "not_white:CS" = "Countering Stereotypes $\\times$ Not White",
    "CH:not_white" = "Common Humanity $\\times$ Not White",
    "news_daily:NM" =  "Norms $\\times$ News: Daily",
    "news_daily:CS" = "Countering Stereotypes $\\times$ News: Daily",
    "CH:news_daily" = "Common Humanity $\\times$ News: Daily"
  ),
  dcolumn = T,
  booktabs = T,
  include.adjrs = F,
  include.rsquared = F,
  include.rmse = F,
  caption = "Exploratory Heterogeneity Analysis",
  caption.above = T,
  label = "tab:subgroups",
  fontsize = "footnotesize",
  stars = c(0.01, 0.05, 0.10),
  digits = 3,
  custom.model.names = paste0("(",1:6,")")
)

